//
//  RootViewController.m
//  PokerHand
//
//  Created by mrbradle on 10/10/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "RootViewController.h"
#import "SetCardViewController.h"
#import "Card.h"

@implementation RootViewController

@synthesize checkLabel, pokerHand;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    cardImages = [NSMutableArray arrayWithCapacity:52];
    for (int i = 1; i <= 52; i++) {
        
        //[cardImages addObject:[UIImage imageWithContentsOfFile:
        //                       [NSString stringWithFormat:@"classic-cards/%d.png",i]]];
        [cardImages addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d",i]]];
    }
    
    pokerHand = [PokerHand alloc];
    pokerHand.cards = [NSMutableArray arrayWithCapacity:5];
    //[cards retain];
    cardButtons = [NSMutableArray arrayWithCapacity:5];
    [cardButtons retain];
    
    for (int i = 0; i < 5; i++) {
        Card* temp = [Card cardWithValue:i suit:0];
        [pokerHand.cards addObject:temp];
        [temp retain];
        
        //Putting the buttons in here "manually" because I can't figure out any other way to put them in an array
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        
        [button setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal];
        [button addTarget:self 
                   action:@selector(setButtonPressed:) 
         forControlEvents:UIControlEventTouchUpInside];
        switch (i) { //the lazy way
            case 0: button.frame = CGRectMake(20, 20, 72, 96);
                break;
            case 1: button.frame = CGRectMake(124, 20, 72, 96);
                break;
            case 2: button.frame = CGRectMake(228, 20, 72, 96);
                break;
            case 3: button.frame = CGRectMake(74, 136, 72, 96);
                break;
            default: button.frame = CGRectMake(178, 136, 72, 96);
        }
        
        [button setImage:[cardImages objectAtIndex:(temp.suit+(12-temp.value)*4)]
                forState:UIControlStateNormal];
        
        //[temp release];
        
        [self.view addSubview:button];
        [cardButtons addObject:button];
        
    }
    
    //Card* theCard = [Card alloc];
    //theCard.value = 0;
    //theCard.suit = 0;
    //card = theCard;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (IBAction)setButtonPressed:(id)sender {
    
    //test card
    //Card* theCard = [Card alloc];
    //theCard.value = 0;
    //theCard.suit = 0;
    
    //Push a SetCardViewController onto the current view    
    
    SetCardViewController* setCardVC =
    [[SetCardViewController alloc] initWithNibName:@"SetCardViewController" bundle:nil
                                              card:[pokerHand.cards
                                                    objectAtIndex:[self findCardButtonIndex:sender]] 
                                            button:sender
                                              hand:pokerHand];
    
    [setCardVC.view setFrame:[[UIScreen mainScreen] applicationFrame]];
     
    [self.navigationController pushViewController:setCardVC animated:YES];
    
}

- (int)findCardButtonIndex:(UIButton*) button {
    
    for (int i = 0; i < 5; i++) {
        if ([cardButtons objectAtIndex:i] == button) return i;
    }
    return -1;
    
}

- (IBAction)checkButtonPressed {
    
    int hand = [pokerHand check];
    
    switch (hand) {
        case 9:
            checkLabel.text = @"Royal flush";
            break;
        case 8:
            checkLabel.text = @"Straight flush";
            break;
        case 7:
            checkLabel.text = @"Four of a kind";
            break;
        case 6:
            checkLabel.text = @"Full house";
            break;
        case 5:
            checkLabel.text = @"Flush";
            break;
        case 4:
            checkLabel.text = @"Straight";
            break;
        case 3:
            checkLabel.text = @"Three of a kind";
            break;
        case 2:
            checkLabel.text = @"Two pair";
            break;
        case 1:
            checkLabel.text = @"Pair";
            break;
        default:
            checkLabel.text = @"Nothing";
    }
    
}

/*
 // Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations.
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
 

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 0;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell.
    return cell;
}*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert)
    {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    /*
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
    // ...
    // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
	*/
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}

- (void)dealloc
{
    [super dealloc];
}

@end
